//
//  FSPrePayVC.m
//  FieldStory
//
//  Created by itte on 2017/6/23.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "FSPrePayVC.h"
#import "PrePayProductCell.h"
#import "FSAddressListVC.h"

@interface FSPrePayVC ()<UITableViewDelegate,UITableViewDataSource,FSAddressDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (strong, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UIButton *btnAddAddr;
@property (weak, nonatomic) IBOutlet UIView *addrView;
@property (weak, nonatomic) IBOutlet UILabel *lbRecvName;
@property (weak, nonatomic) IBOutlet UILabel *lbRecvPhone;
@property (weak, nonatomic) IBOutlet UILabel *lbAddr;
@property (weak, nonatomic) IBOutlet UILabel *lbSumMoney;
@property (weak, nonatomic) IBOutlet UILabel *lbYunFei;
@property (weak, nonatomic) IBOutlet UIButton *btnWallet;
@property (weak, nonatomic) IBOutlet UIButton *btnWx;
@property (weak, nonatomic) IBOutlet UIButton *btnAlipay;
@property (weak, nonatomic) IBOutlet UIButton *btnPay;
@property (strong, nonatomic) NSArray *dataArray;
@property (assign, nonatomic) CGFloat sumPrice;
@property (assign, nonatomic) NSInteger addressID;
@end

@implementation FSPrePayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"结算";
    self.dataArray = self.params[@"data"];
    self.btnPay.layer.cornerRadius = 3;
    [self.tableView registerNib:[UINib nibWithNibName:@"PrePayProductCell" bundle:nil] forCellReuseIdentifier:@"PrePayProductCellID"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.addrView.hidden = YES;
    
    [self changePayway:self.btnWallet];
    [self.tableView reloadData];
    self.sumPrice = [self calculatePrice:self.dataArray];
    self.lbSumMoney.text = [NSString stringWithFormat:@"￥%.2f",self.sumPrice];
    self.lbYunFei.text = @"￥0.00";
    WeakSelfType blockSelf = self;
    [self.addrView bk_whenTapped:^{
        [blockSelf btnAddrClick];
    }];
                            
}

-(CGFloat)calculatePrice:(NSArray *)products
{
    CGFloat sumMoney = 0.0f;
    for (FSShopListModel *shop in products) {
        sumMoney += ([shop.vip_price.trimString floatValue]*shop.payNum);
    }
    return sumMoney;
}
#pragma mark - 支付方式选择
- (IBAction)changePayway:(UIButton *)sender
{
    [sender setImage:[UIImage imageNamed:@"icon_point_yellow"] forState:UIControlStateNormal];
    if (sender.tag == 1000) {
        [self.btnWx setImage:[UIImage imageNamed:@"icon_point_gray"] forState:UIControlStateNormal];
        [self.btnAlipay setImage:[UIImage imageNamed:@"icon_point_gray"] forState:UIControlStateNormal];
    }
    else if (sender.tag == 2000){
        [self.btnWallet setImage:[UIImage imageNamed:@"icon_point_gray"] forState:UIControlStateNormal];
        [self.btnAlipay setImage:[UIImage imageNamed:@"icon_point_gray"] forState:UIControlStateNormal];
    }
    else{
        [self.btnWx setImage:[UIImage imageNamed:@"icon_point_gray"] forState:UIControlStateNormal];
        [self.btnWallet setImage:[UIImage imageNamed:@"icon_point_gray"] forState:UIControlStateNormal];
    }
}
// 支付
- (IBAction)btnPayClick
{
}

#pragma mark - 地址相关
// 添加地址
- (IBAction)btnAddrClick
{
    FSAddressListVC *address = [[FSAddressListVC alloc] init];
    address.delegate = self;
    [self.navigationController pushViewController:address animated:YES];
}

// 获取地址
-(void)selectAddress:(FSAddressModel *)address
{
    self.addressID = address.ID;
    self.lbRecvName.text = address.username;
    self.lbRecvPhone.text = address.phone;
    self.lbAddr.text =[NSString stringWithFormat:@"%@ %@ %@ %@",address.province,address.city,address.district,address.address_detail];
    self.btnAddAddr.hidden = YES;
    self.addrView.hidden = NO;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PrePayProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrePayProductCellID"];
    if (cell == nil) {
        cell = [[PrePayProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PrePayProductCellID"];
    }
    
    FSShopListModel *shop = self.dataArray[indexPath.row];
    [cell.imgProduct sd_setImageWithURL:[NSURL URLWithString:shop.images] placeholderImage:[UIImage imageNamed:@"icon_wx"]];
    cell.lbName.text = shop.name;
    cell.lbWeight.text = [shop.weight addString:@"kg"];
    cell.lbMoney.text = [@"￥" addString:shop.vip_price];
    cell.lbNum.text = [NSString stringWithFormat:@"x%ld",shop.payNum];
    return cell;
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return self.footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 250;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 216;
}
@end
